import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Delete from '#cpt/temp/Delete'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
const boardStatus = [{name: '启用', value: '1'}, {name: '禁用', value: '-1'}]
const board_type = [{name: '96板', value: '96'}, {name: '48板', value: '48'}, {name: '16板', value: '16'}]

class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '编号',			name: 'board_num' },
			{ label: '类型 ',		name: 'board_type' },
			{ label: '状态',			name: 'status', 	type: 'select', data: boardStatus },
		],
		// 提交表单列表
		formData : [
			{ label: '编号',			name: 'board_num', 		required:true, half:true },
			{ label: '类型',			name: 'board_type', 	required:true, half:true,	type: 'select',	data: board_type },
		]
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
			this.fetch()
		}
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'board/index')
	
	// 表单
	cols = [
		{ title: '板子编号', 	dataIndex: 'board_num', 		width:150 },
		{ title: '板子类型', 	dataIndex: 'board_type_name', 	width:200 },
		{ title: '板子状态', 	dataIndex: 'status_name', 		...$fn.table.status },
		TableControls.controls.call(this,{ width: 400 })
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid, status}) => {
		return [
			TableControls.edit.call(this, 'board/info', { uuid }),
			TableControls.click.call(this,  status === '1' ? '禁用' : '启用', () => {
				$http.submit(this, 'board/status', { param:{ uuid: uuid, status: status === '1' ? '-1' : '1' }, dataName: null }).then(() => this.fetch())
			}),
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'board/del', { uuid, isKeys: !uuid })
	
	// 操作按钮
	controls = () => [
		Controls.add.call(this),			// 添加
		this.onDel(),						// 删除
	]
	
	render(){
		return (
			<Page title='板子管理' controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isInit:window.isInit}) }
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'board/add', editApi:'board/edit' }) }
			</Page>
		)
	}
}

export default Index